package capcop;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> accounts = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);
    private Account logaccount = new Account();

    public void start() {
        while (true) {
            System.out.println("=======系统用户界面=======");
            System.out.println("1.用户登入");
            System.out.println("2.用户开户");
            System.out.println("3.退出界面");
            System.out.println("请输入操作：");

            String choice = scanner.next();
            switch (choice) {
                case "1":
                    login();
                    break;
                case "2":
                    createAccount();
                    break;
                case "3":
                     System.out.println("退出成功");
                     return;
                default:
                    System.out.println("没有该操作");
            }
        }
    }
    private void login() {
        if(accounts.size()==0){
            System.out.println("暂无账户，请先开户");
            return;
        }
        System.out.println("==登入系统==");
        System.out.println("请输入卡号");
        String cardID = scanner.next();
        Account acc = getAccountByCardID(cardID);
        if(acc == null){
            System.out.println("请输入正确卡号");
        }
        else{
            while (true){
                System.out.println("请输入登入密码");
                String password = scanner.next();
                if(acc.getPassword().equals(password)){
                    logaccount = acc;
                    System.out.println(acc.getName()+"您已登入成功，您的卡号是："+acc.getCardID());
                    showUserCommand();
                    return;
                }
                else {
                    System.out.println("密码错误,请重新输入");
                }
            }
        }
    }

    private void showUserCommand(){
        while(true) {
            System.out.println("=====您可选择如下操作=====");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销用户");
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    showAccount();
                    break;
                case "2":
                    depsitmoney();
                    break;
                case "3":
                    drawmoney();
                    break;
                case "4":
                    transfermoney();
                    break;
                case "5":
                    changepassword();
                    return;
                case "6":
                    System.out.println("退出成功");
                    return;
                case "7":
                    if(deletaccount()) {
                        return;
                    }
                    break;
                default:
                    System.out.println("没有该操作");
            }
        }
    }

    private void changepassword() {
        System.out.println("==修改密码==");
        while(true) {
            System.out.println("请输入旧密码");
            String password = scanner.next();
            if (password.equals(logaccount.getPassword())) {
                while (true) {
                    System.out.println("请输入新密码");
                    String newpassword = scanner.next();
                    System.out.println("请确认密码");
                    String newpassword2 = scanner.next();
                    if (newpassword.equals(newpassword2)) {
                        logaccount.setPassword(newpassword);
                        System.out.println("密码修改成功");
                        return;
                    } else {
                        System.out.println("两次输入密码不同");
                    }
                }
            } else {
                System.out.println("密码错误");
            }
        }
    }

    private boolean deletaccount() {
        System.out.println("==注销账户==");
            System.out.println("1.确认销户");
            System.out.println("2.退出");
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    System.out.println("Y确认销户，N取消销户");
                    String choice2 = scanner.next();
                    switch(choice2) {
                        case "Y":
                            if (logaccount.getMoney() != 0) {
                                System.out.println("余额还有" + logaccount.getMoney() + "无法销户");
                                return false;
                            } else {
                                accounts.remove(logaccount);
                                System.out.println("销户成功");
                                return true;
                            }
                        default:
                            System.out.println("操作已取消");
                            return false;
                    }
                case "2":
                    System.out.println("退出成功");
                    return false;
                default:
                    System.out.println("没有该操作");
                    return false;
            }
    }

    //转账
    private void transfermoney() {
        System.out.println("==转账==");
        if(accounts.size()<2){
            System.out.println("当前只有一个账户");
            return;
        }
        if (logaccount.getMoney()<0){
            System.out.println("余额为0，无法转账");
            return;
        }
        System.out.println("请输入对方账户：");
        String cardID = scanner.next();
        Account acc = getAccountByCardID(cardID);
        if(acc==null){
            System.out.println("不存在此账户");
        }
        else {
            String name ="*"+acc.getName().substring(1);
            System.out.println("请输入"+name+"的姓氏：");
            String firstname = scanner.next();
            if(acc.getName().startsWith(firstname)){
                System.out.println("请输入转账金额：");
                double money = scanner.nextDouble();
                if(money>logaccount.getMoney()) {
                    System.out.println("余额不足");
                }
                else {
                    acc.setMoney(acc.getMoney() + money);
                    logaccount.setMoney(logaccount.getMoney() - money);
                    System.out.println("转账成功你的余额为" + logaccount.getMoney() + "转账金额为" + money);
                }
            }
            else {
                System.out.println("姓氏错误");
            }
        }
    }

    //存款
    private void depsitmoney(){
        System.out.println("==存钱==");
        System.out.println("请输入存入金额：");
        double sc = scanner.nextDouble();
        double money = logaccount.getMoney();
        logaccount.setMoney(money + sc);
        System.out.println("存入成功，存入金额为："+money+"余额为："+logaccount.getMoney());
    }
    //取款
    private void drawmoney() {
        if (logaccount.getMoney() < 100) {
            System.out.println("当前金额小于100，无法取款");
            return;
        }
    while (true){
        System.out.println("==取款==");
        System.out.println("请输入取出金额：");
        double sc = scanner.nextDouble();
        if (sc > logaccount.getMoney()) {
            System.out.println("金额不足");
            return;
        } else {
            if (sc < logaccount.getLimit()) {
                double money = logaccount.getMoney();
                logaccount.setMoney(money - sc);
                System.out.println("取出成功，取出金额为：" + money + "余额为：" + logaccount.getMoney());
                break;
            } else {
                System.out.println("已经超出限额,限额为：" + logaccount.getLimit());
                return;
            }
        }
        }
    }




        private void showAccount(){
        System.out.println("当前账户信息如下:");
        System.out.println("卡号:"+logaccount.getCardID());
        System.out.println("性别:"+logaccount.getGender());
        System.out.println("户主:"+logaccount.getName());
        System.out.println("余额:"+logaccount.getMoney());
        System.out.println("额度:"+logaccount.getLimit());
    }

    private void createAccount() {
        Account acc = new Account();

        System.out.println("请输入账户名称");
        String name = scanner.next();
        acc.setName(name);

        while (true) {
            System.out.println("请输入性别");
            char gender = scanner.next().charAt(0);
            if (gender == '男' || gender == '女') {
                acc.setGender(gender);
                break;
            } else {
                System.out.println("请输入正确的性别");
            }
        }

        while (true) {
            System.out.println("请输入密码");
            String password = scanner.next();
            System.out.println("请确认密码");
            String password2 = scanner.next();
            if (password.equals(password2)) {
                acc.setPassword(password);
                break;
            } else {
                System.out.println("两次输入密码不同");
            }
        }

        System.out.println("请输入取现额度");
        double limit = scanner.nextDouble();
        acc.setLimit(limit);

        String newCardID = createcardID();
        acc.setCardID(newCardID);

        accounts.add(acc);
        System.out.println("恭喜你，" + acc.getName() + "开户成功，您的卡号是："+ acc.getCardID());
    }
// 随机生成卡号
    private String createcardID() {
        while (true) {
            String cardID = "";
            Random random = new Random();
            for (int i = 0; i < 8; i++) {
                int data = random.nextInt(10);
                cardID +=  data;
            }
            Account acc = getAccountByCardID(cardID);
            if (acc == null) {
                return cardID;
            }
        }
    }

    // 查找卡号
    private Account getAccountByCardID(String cardID) {
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardID().equals(cardID)) {
                return acc;
            }
        }
        return null;
    }


}



